﻿Imports System.Data
Imports System.Data.OleDb
Imports validaciones.metodos
Public Class PedirCita

    Public conexion As New OleDb.OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0; Data Source=nuevozoo.accdb")
    Public adaptador As New OleDbDataAdapter("Select * from consultas", conexion)
    Public miDataset As New DataSet
    Public validacion As New validaciones.metodos

    Private Sub PedirCita_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        lab_Usuario.Text = Bienvenida.usuario

        adaptador.Fill(miDataset, "consultas")
        If BusquedaClientesAnimales.animal <> 0 Then
            text_CodAnimal.Text = BusquedaClientesAnimales.animal
            text_CodAnimal.Enabled = False
        End If
    End Sub

    Private Sub b_Aceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b_Aceptar.Click

        Try
            'Con estas variables creo la hora actual, fecha actual, hora seleccionada y fecha seleccionada
            'y compare el lapso de tiempo entre ambas, si es menor que cero en fecha o en hora siendo la fecha actual
            'saltará una excepcion
            Dim hora_selecc As Date = DateTimePicker2.Value.Hour & ":" & DateTimePicker2.Value.Minute
            Dim hora_acc As Date = Date.Now.Hour & ":" & Date.Now.Minute
            Dim fecha As Long = DateDiff(DateInterval.Day, Date.Now, DateTimePicker1.Value)
            Dim hora As Long = DateDiff(DateInterval.Minute, hora_acc, hora_selecc)

            If fecha < 0 Then

                Throw New ExcepcionFecha
            ElseIf fecha = 0 And hora < 0 Then
                Throw New ExcepcionFecha

            Else
                If text_CodAnimal.Text = "" Then
                    Throw New Exception("Debe indicar un código de animal")
                End If
                'sacar ultimo codigo de empleados usado
                Dim cmd As New OleDbCommand("select max(cod_consulta) from consultas", conexion)
                conexion.Open()
                Dim a As Object = cmd.ExecuteScalar
                Dim codMaxUsado As String = a.ToString
                Dim codNuevo As Integer = codMaxUsado + 1 'codigo del nuevo registro

                Dim cb As New OleDbCommandBuilder(adaptador)
                adaptador.InsertCommand = cb.GetInsertCommand
                Dim mitabla As DataTable = miDataset.Tables("consultas")
                Dim dr As DataRowCollection = mitabla.Rows
                Dim nuevafila As DataRow
                nuevafila = mitabla.NewRow

                dr.Add(codNuevo, text_CodAnimal.Text, DateTimePicker1.Value.Date, text_Motivo.Text, 0, DateTimePicker2.Value.Hour & ":" & DateTimePicker2.Value.Minute)

                adaptador.Update(mitabla)
                miDataset.Clear()
                adaptador.Fill(miDataset, "consultas")
                MsgBox("Cita insertada con éxito", , "Correcto")
                Me.Close()
                GestionGeneral.Show()
            End If
        Catch ex2 As ExcepcionFecha
            MsgBox("La fecha indicada ya ha pasado", , "Error")
            Bienvenida.log.WriteLine("Fecha: " & Date.Now & "Pantalla: " & Me.Name & " Error: " & ex2.Message)
            Bienvenida.log.Flush()
        Catch ex As Exception
            MsgBox(ex.Message, , "Error")
            Bienvenida.log.WriteLine("Fecha: " & Date.Now & "Pantalla: " & Me.Name & " Error: " & ex.Message)
            Bienvenida.log.Flush()
        End Try
        conexion.Close()
    End Sub

    Private Sub text_CodAnimal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles text_CodAnimal.TextChanged
        Try
            If validacion.numeros(text_CodAnimal.Text) <> True And text_CodAnimal.Text <> "" Then
                text_CodAnimal.Text = ""
                Throw New Exception("El campo debe ser numérico")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, , "Advertencia")
        End Try
        
    End Sub

    Private Sub link_Cerrar_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
        Bienvenida.usuario = ""
        Bienvenida.Show()
        Me.Close()
    End Sub

    Private Sub VolverToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VolverToolStripMenuItem.Click
        GestionGeneral.Show()
        Me.Close()
    End Sub
End Class